package com.onion.utils.http;

import com.alibaba.fastjson.JSON;
import org.apache.http.client.methods.HttpUriRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class HttpCallback<T> {

    private Logger logger = LoggerFactory.getLogger(HttpCallback.class);

    private T callbackHandler;

    public HttpCallback(){}

    public HttpCallback(T callbackHandler){
        this.callbackHandler = callbackHandler;
    }

    public abstract void completed(HttpUriRequest request, HttpResult<String> httpResult);

    public void failed(HttpUriRequest request, HttpResult httpResult, Exception exp) {
        if(httpResult == null){
            logger.error("HTTP请求异常: url={}", request.getURI().toString(), exp);
        }else{
            logger.error("HTTP请求异常: url={}, result={}", request.getURI().toString(), JSON.toJSONString(httpResult), exp);
        }
    }

    public void cancelled(HttpUriRequest request) {
        logger.warn("HTTP请求取消:{}", request.getURI().toString());
    }

}